package com.angel.webapp.keyValue;

import com.angel.resourceBundle.propertyValue.KeyValue;
import com.angel.resourceBundle.results.KeyValueResult;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author William
 * @since 03/February/2009
 */
public class RequestKeyValue implements KeyValueResult{

    private Map<Object, String> mapParameters;

    @SuppressWarnings({"unchecked"})
    public RequestKeyValue(HttpServletRequest req){
        super();
        mapParameters = new HashMap<Object, String>();
        mapParameters.putAll(req.getParameterMap());
    }

    public Object getResultFor(Object o) {
        return this.mapParameters.get(o);
    }

    public Object getResultNullSafeFor(Object o) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public Object getResultNullSafeFor(Object o, Object o1) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @SuppressWarnings({"unchecked"})
    public <T> Collection<T> findAllResults() {
        return (Collection<T>) mapParameters.values();
    }

    @SuppressWarnings({"unchecked"})
    public <T> Collection<T> findAllKeys() {
        return (Collection<T>) mapParameters.keySet();
    }

    @SuppressWarnings({"UnnecessaryBoxing"})
    public Long resultSize() {
        return new Long(mapParameters.size());
    }

    public Boolean hasResult() {
        return this.resultSize() > 0;
    }

    public Boolean hasResultSize(Long aResultSize) {
        return this.resultSize().longValue() == aResultSize.longValue();
    }

    public void addKeyValueResult(KeyValueResult keyValueResult) {
        for(Object key: keyValueResult.findAllKeys()){
            mapParameters.put(key, keyValueResult.getResultFor(key).toString());
        }
    }

    public <T extends KeyValue> void addAll(Collection<T> ts) {
        for(KeyValue keyValue: ts){
            this.mapParameters.put(keyValue.getKey(), keyValue.getValue());
        }
    }

    public Boolean containsResultFor(Object o) {
        return this.getResultFor(o) != null;
    }
}